local table_insert = table.insert

local Mpq = {}


Mpq.w2l = nil

local mdx_filetype = {
    ['blp'] = true,
    ['mdx'] = true,
    ['tga'] = true,
}

function Mpq.init()
    local path = Utility.Get_absolute_path( 'script\\mpq\\listfile.txt' )
    local file = io.open( path, 'r' )
    local all_files = {}
    local count = 0
    for line in file:lines() do
        local filetype = Utility.Get_filetype(line)
        if filetype and mdx_filetype[filetype:lower()] then
            table_insert( all_files, line )
            count = count + 1
        end
    end
    table.sort( all_files, function ( second, first )
        local filetype_2 = Utility.Get_filetype(second)
        local filetype_1 = Utility.Get_filetype(first)
        return filetype_2 < filetype_1
    end)

    Mpq.origin_file_map = {}
    for _, path in ipairs(all_files) do
        Mpq.origin_file_map[path:lower()] = true
    end

    if Utility.is_lni_map( INPUT_PATH ) then
        Mpq.is_lni_map = true
        require 'script.mpq.w2l._init'
    
        Mpq.w2l = require 'script.mpq.w2l.w2l'
        local slk = {}
        Mpq.w2l:frontend(slk)
    end
    
end

--是否是自定义的资源
function Mpq.is_custom( blp_path )
    return not Mpq.is_origin(blp_path)
end
--是否是自带资源
function Mpq.is_origin( path )
    return Mpq.origin_file_map[path:lower()]
end

function Mpq.analysis_doo()
    local s_path = ([[%s\map]]):format(INPUT_PATH)
    local path = fs.path(s_path)
    if not fs.exists( path ) then
		log.error('只支持 w3x2Lni 格式文件夹')
		return false
	end
    local file_path = path / 'war3map.doo'
	if not fs.exists( file_path ) then
		log.warn('目标文件夹内不存在 war3map.doo')
		return false
    end
    
    local doo = Doodads.new( file_path:string() )
    doo:load()
    
    return doo
end

return Mpq